package topic;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
import utils.RabbitMQUtils;

import java.io.IOException;

/**
 * Customer2
 * '#'消费多个结尾
 * @author 丛有志
 * @date 2020/11/8 23:50
 * <p>
 * 版本        修改时间        作者        修改内容
 * V1.0        2020/11/8     丛有志        初始版本
 */
public class Customer2 {
    public static void main(String[] args) throws IOException {
        // 获取连接
        Connection connection = RabbitMQUtils.getConnection();
        // 获取通道
        Channel channel = connection.createChannel();
        // 声明交换机及交换机类型
        channel.exchangeDeclare("topics", "topic");
        // 创建临时队列
        String queue = channel.queueDeclare().getQueue();
        // 绑定队列和交换机 动态通配符形式route key
        channel.queueBind(queue, "topics", "user.#");
        // 消费消息
        channel.basicConsume(queue, true, new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("消费者1：" + new String(body));
            }
        });

    }
}
